import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:syn_4s/core/extension/int_extension.dart';
import 'package:syn_4s/core/model/project_model.dart';

import '../../core/model/short_model.dart';
import '../shared/app_theme.dart';

// 圆角橙色边框
InputBorder getFocusedBorder() {
  return OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(24.px)),
      borderSide: const BorderSide(color: AppTheme.orangeColor));
}

// 圆角白色边框
InputBorder getWhiteOvalBorder({double? radius}) {
  return OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(radius ?? 24.px)),
      borderSide: const BorderSide(color: Colors.white));
}

Widget getMapTitleText(String data) {
  return Text(
    data,
    style: TextStyle(fontSize: 13.px),
  );
}

Widget getSvgPicture(String src, double size) {
  return SvgPicture.asset(
    src,
    width: size,
    height: size,
  );
}

// 获取widget所在位置
double getDy(GlobalKey key) {
  if (key.currentContext != null) {
    RenderBox renderBox = key.currentContext?.findRenderObject() as RenderBox;
    return renderBox.getTransformTo(null).getTranslation().y;
  }
  return 0;
}

PreferredSizeWidget getAppBar(String title) {
  return PreferredSize(
    preferredSize: Size.fromHeight(62.px),
    child: Container(
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            colors: [AppTheme.appBarStartColor, AppTheme.appBarEndColor]),
      ),
      child: AppBar(
        title: Text(title),
        actions: [
          InkWell(
            onTap: () {},
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 8.px),
              child: Image.asset(
                "assets/images/logo.png",
                width: 92.px,
              ),
            ),
          )
        ],
      ),
    ),
  );
}

void addScrollListener(ScrollController controller, RxBool value) {
  controller.addListener(() {
    // 滚动到边缘时，禁用其滑动并激活 ScrollView 滑动
    if (controller.position.atEdge) {
      SchedulerBinding.instance.addPostFrameCallback((_) {
        value.value = false;
      });
    }
  });
}

void changeScrollStatus(
    ScrollController controller, GlobalKey key, RxBool value) {
  // ScrollView 滚动到 ListView 的位置时，重新启用 ListView 滑动
  final offset = getDy(key);
  if (controller.offset >= offset && !value.value) {
    SchedulerBinding.instance.addPostFrameCallback((_) {
      value.value = true;
    });
  }
}

// 获取showMenu显示位置在下方
RelativeRect getPopupPosition(BuildContext context, GlobalKey key) {
  // 在清除焦点后，延迟执行获取 RenderBox
  final RenderBox button = key.currentContext?.findRenderObject()! as RenderBox;
  final RenderBox overlay =
      Navigator.of(context).overlay!.context.findRenderObject()! as RenderBox;

  Offset offset = Offset(0.0, button.size.height);

  return RelativeRect.fromRect(
    Rect.fromPoints(
      button.localToGlobal(offset, ancestor: overlay),
      button.localToGlobal(button.size.bottomRight(Offset.zero) + offset,
          ancestor: overlay),
    ),
    Offset.zero & overlay.size,
  );
}

Future<ShortModel?> showShortMenu(
    BuildContext context, List<ShortModel> data, GlobalKey key,
    {double? width}) {
  return showMenu<ShortModel>(
      context: context,
      position: getPopupPosition(context, key),
      constraints: BoxConstraints(
          minWidth: width ?? 200.px,
          maxWidth: width ?? 200.px,
          maxHeight: 200.px),
      items: data.map((value) {
        return PopupMenuItem<ShortModel>(
          value: value,
          child: Row(
            children: [
              Expanded(
                  child: Text(
                value.name ?? "",
                style: TextStyle(
                    color: (value.isSelect ?? false)
                        ? AppTheme.orangeColor
                        : AppTheme.norTextColors),
              )),
              Offstage(
                offstage: !(value.isSelect ?? false),
                child: const Icon(
                  Icons.check,
                  color: AppTheme.orangeColor,
                ),
              )
            ],
          ),
        );
      }).toList());
}

Future<ProjectModel?> showProjectMenu(
    BuildContext context, List<ProjectModel> data, GlobalKey key,
    {double? width}) {
  print(data.length);
  return showMenu<ProjectModel>(
      context: context,
      position: getPopupPosition(context, key),
      constraints: BoxConstraints(
          minWidth: width ?? 200.px,
          maxWidth: width ?? 200.px,
          maxHeight: 200.px),
      items: data.map((value) {
        return PopupMenuItem<ProjectModel>(
          value: value,
          child: Row(
            children: [
              Expanded(
                  child: Text(
                    value.name ?? "",
                    style: TextStyle(
                        color: (value.isSelect ?? false)
                            ? AppTheme.orangeColor
                            : AppTheme.norTextColors),
                  )),
              Offstage(
                offstage: !(value.isSelect ?? false),
                child: const Icon(
                  Icons.check,
                  color: AppTheme.orangeColor,
                ),
              )
            ],
          ),
        );
      }).toList());
}
